package com.tuanchau.englishtooltip.store;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.tuanchau.englishtooltip.Global;

public class DatabaseController {
	private static DatastoreService datastore = DatastoreServiceFactory
			.getDatastoreService();

	public static void putWord(Word w) {
		datastore.put(w.getEntity());
	}

	public static void putWord(String word, String detail) {
		Word w = new Word(word, detail);
		datastore.put(w.getEntity());
	}

	public static void putWords(List<Word> words) {
		for (Word w : words) {
			datastore.put(w.getEntity());
		}
	}

	public static void putWords(Word[] words) {
		for (Word w : words) {
			datastore.put(w.getEntity());
		}
		
	}

	public static Word getEntity(String word) {
		Word w = null;
		try {
			Key k = KeyFactory.createKey(Global.TABLE_WORD_NAME, word);
			Entity e = datastore.get(k);
			w = new Word(e);
		} catch (Exception e1) {
		}
		return w;
		
	}
	
	public static List<Word> getEntities(String word) {
		Query q = new Query(Global.TABLE_WORD_NAME);
		
		q.addFilter(Global.COL_WORD, Query.FilterOperator.EQUAL, word)
				.addSort(Global.COL_WORD).addSort(Global.COL_TYPE)
				.addSort(Global.COL_MEAN);
		PreparedQuery pq = datastore.prepare(q);
		

		ArrayList<Word> words = new ArrayList<Word>(3);
		

		for (Entity e : pq.asIterable()) {
			Word w = new Word(e);
			words.add(w);
		}

		return words;

	}

	public static List<Word> getEntities(String word, String type) {
		Query q = new Query(Global.TABLE_WORD_NAME);
		q.addFilter(Global.COL_WORD, Query.FilterOperator.EQUAL, word)
				.addFilter(Global.COL_TYPE, Query.FilterOperator.EQUAL, type)
				.addSort(Global.COL_WORD).addSort(Global.COL_TYPE)
				.addSort(Global.COL_MEAN);
		PreparedQuery pq = datastore.prepare(q);

		ArrayList<Word> words = new ArrayList<Word>(3);

		for (Entity e : pq.asIterable()) {
			Word w = new Word(e);
			words.add(w);
		}

		return words;
	}
	
	public static void resetDatabase() {
		LinkedList<Key> keys = new LinkedList<Key>();
		Query q = new Query(Global.TABLE_WORD_NAME);
		q.addFilter(Global.COL_WORD, Query.FilterOperator.NOT_EQUAL, "");
		PreparedQuery pq = datastore.prepare(q);
		for(Entity e:pq.asIterable()) {
			keys.add(e.getKey());
		}
		datastore.delete(keys);
	}
	
	public static String countNumberOfRow() {
		LinkedList<Key> keys = new LinkedList<Key>();
		Query q = new Query(Global.TABLE_WORD_NAME);
		q.addFilter(Global.COL_WORD, Query.FilterOperator.NOT_EQUAL, "");
		PreparedQuery pq = datastore.prepare(q);
		for(Entity e:pq.asIterable()) {
			keys.add(e.getKey());
		}
		String result = "Data length = " + keys.size() + "\r\n";
		try {
			Entity e = datastore.get(keys.getLast());
			Word w = new Word(e);
		
			result += "Last entity = " +  w.toString();
			
			
		} catch (EntityNotFoundException e1) {
			// TODO Auto-generated catch block
			result += "have an error";
		}
		
		return result;
	}
}
